package com.zonski.jbots.game.entity;

import java.util.Vector;

import com.zonski.jbots.game.EntityHelper;
import com.zonski.jbots.game.JBotsConstants;

import com.zonski.jbots.engine.*;
import com.zonski.jbots.engine.entity.AbstractMind;
import com.zonski.jbots.engine.entity.Mind;

public class BombBotMind extends SeekerBotMind
{
    public BombBotMind()
    {
    }

    public void update(Engine engine)
        throws Exception
    {
        if(this.getEntity().getMode() == Modes.DEAD)
        {
            explode(engine);
        }else{
            super.update(engine);
        }
    }

    private final void explode(Engine engine)
        throws Exception
    {
        EntityHelper.addEntity(JBotsConstants.EXPLOSION, getEntity(), engine, getEntity().layerId);
        getEntity().setRemovable(true);
    }

    protected int getDesirability(QuickVector entities, int distance, int direction)
    {
        int enemies = 0;
        if(entities != null)
        {
            for(int i=entities.size(); i > 0; )
            {
                i--;
                Entity entity = (Entity)entities.elementAt(i);
                if(entity.layerId != this.getEntity().layerId)
                {
                    if(JBotsConstants.isBot(entity.entityType.getId()))
                    {
                        enemies++;
                        this.collisionFactor = 0;
                    }
                }
            }
        }
        int desirability;
        if(distance <= 0)
        {
            desirability = -10;
        }else{
            int adj;
            if(distance == 1)
            {
                if(direction == getEntity().direction)
                {
                    adj = 5;
                }else if(direction == Directions.getOppositeDirection(getEntity().direction)){
                    adj = -5;
                }else{
                    adj = random.nextInt()%7;
                }
            }else{
                adj = 0;
            }
            desirability = (enemies*64)/distance + adj;
        }
        return desirability;
    }

    public void collision(Engine engine, CollisionData data)
    {
        // explode
        if(data.with.layerId != this.getEntity().layerId &&
                JBotsConstants.isBot(data.with.entityType.getId()))
        {
            this.getEntity().setMode(Modes.DEAD);
        }
        super.collision(engine, data);
    }

    public Mind copy()
    {
        return new BombBotMind();
    }
}
